name: Publish Release Artifacts

on:
  release:
    types: [published]

env:
  IMAGE_NAME: ${{ github.repository }}
  REGISTRY: ghcr.io

jobs:
  gh-release_artifacts:
    runs-on: ubuntu-latest
    name: Publish Artifacts to GitHub Releases

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'temurin'
      - name: Set version
        id: set-version
        run: |
          version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
          echo "project-version=$version" >> $GITHUB_OUTPUT
          echo "PROJECT_VERSION=$version" >> $GITHUB_ENV
      - run: echo $PROJECT_VERSION
      - name: Fetch artifact
        id: fetch-artifact
        run: |
          GROUP_ID=io/jenkins/jenkinsfile-runner
          ARTIFACT_ID=jenkinsfile-runner
          FILE_NAME=jenkinsfile-runner
          PROJECT_VERSION=${{ steps.set-version.outputs.project-version }}
          echo "file-name=$FILE_NAME" >> $GITHUB_OUTPUT
          wget -q https://repo.jenkins-ci.org/releases/$GROUP_ID/$ARTIFACT_ID/$PROJECT_VERSION/$ARTIFACT_ID-$PROJECT_VERSION.zip \
            -O $FILE_NAME-$PROJECT_VERSION.zip
      - name: Upload artifact minimum package
        id: upload-artifact-minimum-package
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./${{ steps.fetch-artifact.outputs.file-name }}-${{ steps.set-version.outputs.project-version }}.zip
          asset_name: ${{ steps.fetch-artifact.outputs.file-name }}-${{ steps.set-version.outputs.project-version }}.zip
          asset_content_type: application/zip
  
  gh-registry-images:
    name: "Deploy Docker Image: ${{ matrix.alias }}"
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - alias: jre-11
            dockerfile: packaging/docker/unix/eclipse-temurin-11-jre/Dockerfile
            tag-prefix: eclipse-temurin-11-jre-
            extra-tags--pattern: ", ghcr.io/${{ github.repository }}:jre-11-$RELEASE_VERSION"
          - alias: jre-11-alpine
            dockerfile: packaging/docker/unix/eclipse-temurin-11-jre-alpine/Dockerfile
            tag-prefix: eclipse-temurin-11-jre-alpine-
            extra-tags-pattern: ", ghcr.io/${{ github.repository }}:alpine-$RELEASE_VERSION, ghcr.io/${{ github.repository }}:jre-11-alpine-$RELEASE_VERSION"
    permissions:
      contents: read
      packages: write
    
    
    #TODO: There is slight double build overhead, but we prevent permissions from being exposed too widely
    steps:
    - uses: actions/checkout@v2
    - name: Determine version
      run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
    - name: Set extra tags
      run: echo "RELEASE_EXTRA_TAGS=${{ matrix.extra-tags }}" >> $GITHUB_ENV 
    - name: Log in to the Container registry
      uses: docker/login-action@v1.10.0
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1.5.0
    
    - name: Extract metadata (tags, labels) for Docker
      id: meta
      uses: docker/metadata-action@v3.3.0
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        flavor: |
          latest=false
          prefix=${{ matrix.tag-prefix }}
          suffix=

    - name: Build and Deploy Docker image
      uses: docker/build-push-action@v2.6.1
      with:
        context: .
        file: ${{ matrix.dockerfile }}
        push: true
        tags: ${{ steps.meta.outputs.tags }} ${{ env.RELEASE_EXTRA_TAGS }}
        labels: ${{ steps.meta.outputs.labels }}
